package com.kuangxiaoyang.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.foodclient.adapter.FoodListAdapter;
import com.example.foodclient.pojo.FoodData;
import com.google.gson.Gson;

import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodListActivity extends AppCompatActivity {

    private static final String TAG = "FoodListActivity";

    private ListView foodListView;
    private List<FoodData.DataBean> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);
        initView();
        initEvent();
        showFoodList();
    }

    private void initEvent() {
        foodListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                String iconName = data.get(position).getIcon();
//                String foodName = data.get(position).getName();
//                double price = data.get(position).getPrice();
//                String description = data.get(position).getDescription();

                Intent intent = new Intent(FoodListActivity.this, FoodDetailActivity.class);

                int foodId = data.get(position).getId();
                intent.putExtra("foodId",String.valueOf(foodId));
                intent.putExtra("position",position);

//                Bundle bundle = new Bundle();

//                bundle.putString("iconName", iconName);
//                bundle.putString("foodName", foodName);
//                bundle.putString("price", String.valueOf(price));
//                bundle.putString("description", description);
//
//                intent.putExtras(bundle);

                startActivity(intent);


            }
        });
    }

    private void initView() {
        foodListView = findViewById(R.id.food_list_view);

    }

    private void showFoodList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient().newBuilder()
                            .build();
                    Request request = new Request.Builder()
                            .url("http://192.168.137.1:8083/findAllFood")
                            .method("GET", null)
                            .build();

                    Response response = client.newCall(request).execute();
                    String result = response.body().string();
                    Log.e(TAG, "run: " + result);

                    Gson gson = new Gson();
                    data = gson.fromJson(result, FoodData.class).getData();


                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            foodListView.setAdapter(new FoodListAdapter(FoodListActivity.this, data));
                        }
                    });
                    Log.i(TAG, "run: " + data);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
